package com.rdtravel.converters;

import com.rdtravel.entities.Trip;
import com.rdtravel.services.TripsService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;

public class StringToTripConverter implements Converter<String, Trip> {
	private final TripsService tripsService;

	@Autowired
	public StringToTripConverter(TripsService tripsService) {
		this.tripsService = tripsService;
	}

	@Override
	public Trip convert(String tripId) {
		if (tripId == null || tripId.isEmpty()) {
			return null;
		}
		return tripsService.findById(Long.parseLong(tripId));
	}

}
